package com.fengyi.library.controller;

import com.fengyi.library.DTO.BookDTO;
import com.fengyi.library.service.BookCircuitBreakerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书查询熔断器控制器
 * 用于测试图书查询熔断降级功能
 */
@Controller
@RequestMapping("/books/circuit-breaker")
public class BookCircuitBreakerController {

    @Autowired
    private BookCircuitBreakerService bookCircuitBreakerService;
    
    /**
     * 显示图书查询熔断器测试页面
     * @param model 模型
     * @return 视图名称
     */
    @GetMapping
    public String showTestPage(Model model) {
        return "books/circuit-breaker-test";
    }
    
    /**
     * 根据ID查询图书（使用熔断器）
     * @param id 图书ID
     * @return 图书信息
     */
    @GetMapping("/book/{id}")
    @ResponseBody
    public Map<String, Object> getBookById(@PathVariable Integer id) {
        System.out.println("接收到根据ID查询图书请求 - ID: " + id);
        
        Map<String, Object> result = new HashMap<>();
        try {
            BookDTO book = bookCircuitBreakerService.findBookById(id);
            result.put("success", true);
            result.put("book", book);
            result.put("message", "图书查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "图书查询失败: " + e.getMessage());
            result.put("error", e.getClass().getName());
        }
        
        return result;
    }
    
    /**
     * 查询所有图书（使用熔断器）
     * @return 图书列表
     */
    @GetMapping("/books")
    @ResponseBody
    public Map<String, Object> getAllBooks() {
        System.out.println("接收到查询所有图书请求");
        
        Map<String, Object> result = new HashMap<>();
        try {
            List<BookDTO> books = bookCircuitBreakerService.findAllBooks();
            result.put("success", true);
            result.put("books", books);
            result.put("count", books.size());
            result.put("message", "图书查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "图书查询失败: " + e.getMessage());
            result.put("error", e.getClass().getName());
        }
        
        return result;
    }
    
    /**
     * 分页查询图书（使用熔断器）
     * @param page 页码
     * @param size 每页大小
     * @return 分页图书列表
     */
    @GetMapping("/books/page")
    @ResponseBody
    public Map<String, Object> getBooksByPage(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        System.out.println("接收到分页查询图书请求 - 页码: " + page + ", 每页大小: " + size);
        
        Map<String, Object> result = new HashMap<>();
        try {
            Page<BookDTO> bookPage = bookCircuitBreakerService.findBooksByPage(page, size);
            result.put("success", true);
            result.put("books", bookPage.getContent());
            result.put("totalPages", bookPage.getTotalPages());
            result.put("totalElements", bookPage.getTotalElements());
            result.put("currentPage", page);
            result.put("message", "图书查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "图书查询失败: " + e.getMessage());
            result.put("error", e.getClass().getName());
        }
        
        return result;
    }
    
    /**
     * 根据关键字搜索图书（使用熔断器）
     * @param keyword 关键字
     * @param page 页码
     * @param size 每页大小
     * @return 搜索结果
     */
    @GetMapping("/books/search")
    @ResponseBody
    public Map<String, Object> searchBooks(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        System.out.println("接收到搜索图书请求 - 关键字: " + keyword + ", 页码: " + page + ", 每页大小: " + size);
        
        Map<String, Object> result = new HashMap<>();
        try {
            Page<BookDTO> bookPage = bookCircuitBreakerService.searchBooks(keyword, page, size);
            result.put("success", true);
            result.put("books", bookPage.getContent());
            result.put("totalPages", bookPage.getTotalPages());
            result.put("totalElements", bookPage.getTotalElements());
            result.put("currentPage", page);
            result.put("keyword", keyword);
            result.put("message", "图书搜索成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "图书搜索失败: " + e.getMessage());
            result.put("error", e.getClass().getName());
        }
        
        return result;
    }
    
    /**
     * 根据分类查询图书（使用熔断器）
     * @param category 分类
     * @param page 页码
     * @param size 每页大小
     * @return 查询结果
     */
    @GetMapping("/books/category")
    @ResponseBody
    public Map<String, Object> getBooksByCategory(
            @RequestParam String category,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {
        System.out.println("接收到按分类查询图书请求 - 分类: " + category + ", 页码: " + page + ", 每页大小: " + size);
        
        Map<String, Object> result = new HashMap<>();
        try {
            Page<BookDTO> bookPage = bookCircuitBreakerService.findBooksByCategory(category, page, size);
            result.put("success", true);
            result.put("books", bookPage.getContent());
            result.put("totalPages", bookPage.getTotalPages());
            result.put("totalElements", bookPage.getTotalElements());
            result.put("currentPage", page);
            result.put("category", category);
            result.put("message", "图书分类查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "图书分类查询失败: " + e.getMessage());
            result.put("error", e.getClass().getName());
        }
        
        return result;
    }
} 